package com.gzx.plugin.tjzy.modular.blacklist.service.impl;

import com.gzx.plugin.tjzy.modular.blacklist.dto.GzxBlackRespDto;
import com.gzx.plugin.tjzy.modular.blacklist.mapper.GzxBlackListMapper;
import com.gzx.plugin.tjzy.modular.blacklist.service.GzxBlackListDbService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import vip.xiaonuo.auth.core.util.StpLoginUserUtil;
import vip.xiaonuo.common.util.CommonCryptogramUtil;
import vip.xiaonuo.common.util.CommonDateUtil;
import vip.xiaonuo.common.util.CommonSleepUtil;

import java.util.List;
import java.util.stream.Collectors;

import static org.apache.commons.lang.StringEscapeUtils.escapeSql;

/**
 * @author zjt
 * @description
 * @date 2025/5/20 10:05
 */
@Service
@RequiredArgsConstructor
public class GzxBlackListDbServiceImpl implements GzxBlackListDbService {

    private final GzxBlackListMapper blackListMapper;

    /**
     * 批量保存到数据库
     *
     * @param dataRespList
     */
    @Override
    public void saveBatch(List<GzxBlackRespDto.DataResp> dataRespList) {
        String tenantId = StpLoginUserUtil.getLoginUser().getTenantId();
        String userId = StpLoginUserUtil.getLoginUser().getId();
        String todayStr = CommonDateUtil.getTodayStr();
        String batchSql = dataRespList.stream()
                .map(s -> String.format("('%s', %s, '%s', '%s', '%s')",
                        escapeSql(CommonCryptogramUtil.doSm1AesEncrypt(s.getCallee())),
                        escapeSql(String.valueOf(s.getForbid())),
                        escapeSql(todayStr),
                        escapeSql(tenantId),
                        escapeSql(userId)))
                .collect(Collectors.joining(", "));
        blackListMapper.insertBatchSql(batchSql);
        CommonSleepUtil.sleep100();
    }
}
